﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SistemaAcademicoBLL.Util;
using SistemaAcademicoBO.Entities;
using SistemaAcademicoBLL;

namespace SistemaAcademico.Pages
{
    public partial class NewStudent : System.Web.UI.Page
    {
        #region FIELDS
        ApplicationFacade facade = new ApplicationFacade();
        string nome;
        string cpf;
        string RG;
        Guid matricula;
        DateTime dataNasc;
        char sexo;
        string endereco;
        string cep;
        string telefone;
        string email;
        int degreeCourseId;
        char periodo;
        DateTime inicioSemestre;
        #endregion
        #region PAGE EVENTS
        protected void Page_Load(object sender, EventArgs e)
        {
            #region LOAD DEGREE COURSES
            if (!IsPostBack)
            {
                ddlDegreeCourse = UsefullStuffs.LoadAllDegreeCourses(ddlDegreeCourse);
            }
            #endregion

            #region LOAD GUID
            if (!IsPostBack)
            {
                matricula = Guid.NewGuid();
                txtMatricula.Text = matricula.ToString();
            }
            #endregion
        }
        #endregion

        #region CONTROL EVENTS
        protected void btnCadastrar_Click(object sender, EventArgs e)
        {
            if (Validacao())
            {
                GetStudentData();
                if (ValidateLogin(txtLogin.Text))
                {
                    User user = new User();
                    user.UserAlcada = 2;
                    user.UserId = 0;
                    user.UserLogin = txtLogin.Text;
                    user.UserPassword = Criptografia.Criptografa(txtPassword.Text);
                    user.ReferenceId = txtMatricula.Text;
                    facade.SetUser(user);

                    Student student = new Student();
                    student.Nome = nome;
                    matricula = new Guid(txtMatricula.Text);
                    student.MatriculaId = matricula;
                    student.Nascimento = dataNasc;
                    student.Periodo = periodo;
                    student.Sexo = sexo;
                    student.Rg = RG;
                    student.Telefone = telefone;
                    student.Cpf = cpf;
                    student.DegreeCourseId = degreeCourseId;
                    student.DtIngresso = inicioSemestre;
                    student.Endereco = endereco;
                    student.Formado = 0;
                    student.Cep = cep;
                    student.Email = email;
                    student.UserId = facade.GetIdentCurrent("Users");
                    facade.SetStudent(student);
                    Alert.Show("Cadastro feito com sucesso!");
                }
                else
                {
                    Alert.Show("Esse login já está em uso.");
                }
            }
        }



        #endregion

        #region METHODS
        private bool Validacao()
        {
            if (!Validator.ValidateCPF(txtCPF.Text))
            {
                return false;
            }
            return true;
        }
        private void GetStudentData()
        {
            nome = txtNome.Text;
            cpf = txtCPF.Text;
            RG = txtRg.Text;
            try
            {
                dataNasc = Convert.ToDateTime(txtDate.Text);
            }
            catch (Exception)
            {

                Alert.Show("Data de Nascimento inválida");
            }
            sexo = Convert.ToChar(ddlSexo.SelectedValue);
            endereco = txtEndereco.Text;
            cep = txtCep.Text;
            telefone = txtTelefone.Text;
            email = txtEmail.Text;

            degreeCourseId = Convert.ToInt32(ddlDegreeCourse.SelectedValue);
            periodo = Convert.ToChar(ddlPeriodo.SelectedValue);
            try
            {
                inicioSemestre = Convert.ToDateTime(txtInicio.Text);
            }
            catch (Exception)
            {
                Alert.Show("Data de Início inválida");
            }
        }
        private bool ValidateLogin(string login)
        {
            List<User> users = facade.GetAllUsers();
            foreach (User user in users)
            {
                if (user.UserLogin == login)
                {
                    return false;
                }
            }
            return true;
        }

        #endregion
    }
}